package com.shengyuan.manage_os.old.ying.erpm;

import org.json.JSONException;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.controls.PictureButton;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_JiShi_Table;
import com.shengyuan.manage_os.old.ying.erpm.tasks.Submit_System_Setting_Data_Task;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class Actual_Form_GC_JieXieJiaYou extends BaseActivity {

	private Spinner spinner_jixiemingcheng;
	private PictureButton PictureButton_jiayouji, PictureButton_jixiebiao, PictureButton_driver;
	private EditText editText_danjia, editText_shengshu,editText_jixiebiaoshu,editText_youhao;

	@Override
	public void onClick(View view) {
		super.onClick(view);
	}
	@Override
	protected void onResume() {
		if(PictureButton_driver!=null)PictureButton_driver.postInvalidate();
		if(PictureButton_jiayouji!=null)PictureButton_jiayouji.postInvalidate();
		if(PictureButton_jixiebiao!=null)PictureButton_jixiebiao.postInvalidate();
		super.onResume();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actual_form_gc_chejiayou);
		InitControls();
	}

	@Override
	protected void SaveJsonData() {
		try {
			IsJson1 = true; mFinished = 1 ;
			JsonMyID();
			getTask();
			getExtData();
			getCaiLiao();
			getFuWuMingCheng();
			
			getGPS();
			getCurrentUser();		
			getMemo();
			JsonPicture();
			JsonPicture_Ext();
			//价格保存
			if (Submit_System_Setting_Data_Task.mID>=0) {
				String PingJunYou = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("CaiLiaoJiaGe");
				Double dd = Double.parseDouble( editText_danjia.getText().toString());
				Double val =Double.parseDouble( editText_shengshu.getText().toString()) * dd;
				String s = val + "";
				mJson.put( getString(R.string.title_actual_form_yusuanjiage), s);
				mJson.put( getString(R.string.title_actual_form_jiage),  (dd+""));
				mJson.put( "预计油耗:",  PingJunYou);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void getExtData() throws JSONException {
		if (spinner_jixiemingcheng!=null) {
			mJson.put( getString(R.string.title_actual_form_edittext_jixiemingcheng), 
					spinner_jixiemingcheng.getSelectedItem().toString());
		}
		if (editText_danjia!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_tiyou_danjia), editText_danjia.getText().toString());
		}
		if (editText_shengshu!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_tiyou_fayoushuliang), editText_shengshu.getText().toString());
			//ECApplication.pShareUtil.WriteParam(editText_shengshu.getClass().getSimpleName(), editText_shengshu.getText().toString());
		}		
		if (editText_jixiebiaoshu!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_tiyou_jixiebiaoshu), editText_jixiebiaoshu.getText().toString());
			//ECApplication.pShareUtil.WriteParam(editText_jixiebiaoshu.getClass().getSimpleName(), editText_jixiebiaoshu.getText().toString());
		}
		if (editText_DiDian!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_tiyou_didian), editText_DiDian.getText().toString());
		}
		if (editText_SiJi!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_tiyou_siji), editText_SiJi.getText().toString());
		}		
	}

	protected void JsonPicture() throws JSONException {
		if (PictureButton_Car!=null) {
			mJson.put( PictureButton_Car.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_Car.mFileName);
		}
		if (PictureButton_Chexing!=null) {
			mJson.put( PictureButton_Chexing.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_Chexing.mFileName);
		}
		if (PictureButton_CaiLiao!=null) {
			mJson.put( PictureButton_CaiLiao.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_CaiLiao.mFileName);
		}
		if (PictureButton_ZhiLiang!=null) {
			mJson.put( PictureButton_ZhiLiang.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_ZhiLiang.mFileName);
		}
	}

	protected void JsonPicture_Ext() throws JSONException {
		if (PictureButton_jiayouji!=null) {
			mJson.put( PictureButton_jiayouji.FIELD + getString(R.string.title_actual_form_PictureButton)   , PictureButton_jiayouji.mFileName);
		}
		if (PictureButton_jixiebiao!=null) {
			mJson.put( PictureButton_jixiebiao.FIELD + getString(R.string.title_actual_form_PictureButton) , PictureButton_jixiebiao.mFileName);		
		}
		if (PictureButton_driver!=null) {
			mJson.put( PictureButton_driver.FIELD+ getString(R.string.title_actual_form_PictureButton), PictureButton_driver.mFileName);		
		}
	}	
	@Override
	protected void SaveSelfPic(String sPath) {
		try {
			CopyPicDest(PictureButton_driver, sPath);
			CopyPicDest(PictureButton_jiayouji, sPath);
			CopyPicDest(PictureButton_jixiebiao, sPath);
		} catch (Exception e) {
			Utility.MessageBox(this,e.getMessage());
		}
	}

	@Override
	public void Back() {
		super.Back();

	}

	@Override
	public void InitControls() {
		super.InitControls();
		PictureButton_jiayouji = (PictureButton) findViewById(R.id.PictureButton_jiayouji);
		PictureButton_jixiebiao = (PictureButton) findViewById(R.id.PictureButton_jixiebiao);
		PictureButton_driver = (PictureButton) findViewById(R.id.PictureButton_driver);
		
		spinner_jixiemingcheng = (Spinner) findViewById(R.id.spinner_jixiemingcheng);
		
		editText_danjia = (EditText) findViewById(R.id.editText_danjia);
		editText_shengshu = (EditText) findViewById(R.id.editText_shengshu);
		editText_jixiebiaoshu = (EditText) findViewById(R.id.editText_jixiebiaoshu);
		editText_youhao = (EditText) findViewById(R.id.editText_youhao);
		
		
		if (spinner_jixiemingcheng!=null) {
			try {
				String v = 	Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("WaiZuJiXie");
				String jiage = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("YuSuanJiaGe");
				String youhao = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("CaiLiaoJiaGe");
				editText_youhao.setText(youhao);
				if (v!=null) {
					String[] s = v.split("\r\n");
					ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
							R.layout.simple_spinner_item, 
							s);
					adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
					spinner_jixiemingcheng.setAdapter(adapter);
				}else {
					Utility.MessageBox(this,"机械设备名称数据没有下载");
					this.finish();
				}
				if (jiage!=null) {
					editText_danjia.setText(jiage);
					
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	
	@Override
	public Boolean IsPassed() {
		if (super.IsPassed()) {
			if (spinner_jixiemingcheng!=null) {
				String value = spinner_jixiemingcheng.getSelectedItem().toString();
				if (value.equals("")) {
					Utility.MessageBox(this, getResources().getString(R.string.spinner_jixiemingcheng_no));
					return false;
				}else {
			        Actual_JiShi_Table dbo= new Actual_JiShi_Table(this);
			        int state = dbo.getState(value);
			        if (state !=0 ) {
			        	Utility.MessageBox(this,value + ",本机械设备正在运行计时中，请停止计时!");
						return false;
					}
				}
			}
			if(!CheckEditNumber(editText_danjia))
				return false;
			if(!CheckEditNumber(editText_shengshu))
				return false;			
			if(!CheckEditEmpty(editText_jixiebiaoshu))
				return false;			
			if (!PictureButton_driver.IsExist()) {
				Utility.MessageBox(this, PictureButton_driver.FIELD );
				return false;
			}	
			if (!PictureButton_jiayouji.IsExist()) {
				Utility.MessageBox(this, PictureButton_jiayouji.FIELD );
				return false;
			}			
			if (!PictureButton_jixiebiao.IsExist()) {
				Utility.MessageBox(this, PictureButton_jixiebiao.FIELD );
				return false;
			}

			return true;
		}else {
			return false;
		}
	}

}
